package com.yanqu.road.dao.impl.activity.pasture;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.pasture.config.PastureAnimalConfig;
import com.yanqu.road.entity.activity.pasture.config.PastureModuleConfig;
import com.yanqu.road.entity.activity.pasture.config.PasturePracticeConfig;
import com.yanqu.road.entity.activity.pasture.config.PastureSkillConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PastureConfigDaoImpl extends TempDao {

    private static String getPastureAnimalConfigSql = "select * from `t_s_pasture_animal` where `activityId`=? ORDER BY `animalId` ASC";

    public Map<Integer, PastureAnimalConfig> getPastureAnimalConfig(int activityId) {
        Map<Integer, PastureAnimalConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getPastureAnimalConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PastureAnimalConfig data = new PastureAnimalConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setAnimalId(rs.getInt("animalId"));
                data.setSkillId(rs.getInt("skillId"));
                data.setSize(rs.getInt("size"));
                data.setPoint(StringUtils.stringToIntegerList(rs.getString("point"), "\\|"));
                data.setCost(StringUtils.stringToIntegerList(rs.getString("cost"), "\\|"));
                data.setEnergy(rs.getInt("energy"));
                data.setDisplay(rs.getInt("display"));
                data.setBurst(rs.getInt("burst"));
                result.put(data.getAnimalId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getPastureSkillConfigSql = "select * from `t_s_pasture_skill` where `activityId`=? ORDER BY `skillId` ASC";

    public Map<Integer, PastureSkillConfig> getPastureSkillConfig(int activityId) {
        Map<Integer, PastureSkillConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getPastureSkillConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PastureSkillConfig data = new PastureSkillConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setSkillId(rs.getInt("skillId"));
                data.setSkillParam(rs.getString("skillParam"));
                data.setRepeat(rs.getInt("repeat"));
                data.setDesc(rs.getString("desc"));
                result.put(data.getSkillId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getPastureModuleConfigSql = "select * from `t_s_pasture_module` where `activityId`=? ORDER BY `round` ASC";

    public Map<Integer, PastureModuleConfig> getPastureModuleConfig(int activityId) {
        Map<Integer, PastureModuleConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getPastureModuleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PastureModuleConfig data = new PastureModuleConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setRound(rs.getInt("round"));
                data.setModuleList(rs.getString("moduleList"));
                data.setAnimalList(rs.getString("animalList"));
                result.put(data.getRound(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static String getPasturePracticeConfigSql = "select * from `t_s_pasture_practice` where `activityId`=? ORDER BY `model` ASC";

    public Map<Integer, Map<Integer, PasturePracticeConfig>> getPasturePracticeConfig(int activityId) {
        Map<Integer, Map<Integer, PasturePracticeConfig>> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getPasturePracticeConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PasturePracticeConfig data = new PasturePracticeConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setModel(rs.getInt("model"));
                data.setLine(rs.getInt("line"));
                data.setStart(rs.getInt("start"));
                data.setBox(rs.getString("box"));
                Map<Integer, PasturePracticeConfig> modelMap = result.getOrDefault(data.getModel(), new HashMap<>());
                modelMap.put(data.getLine(), data);
                result.put(data.getModel(), modelMap);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


}
